package com.xyhuo.leetcode.january;

import java.util.ArrayList;
import java.util.List;

/**
 * 杨辉三角Ⅱ
 * https://leetcode.cn/problems/pascals-triangle-ii/
 *
 * @time 2024/1/11
 */
public class GetRow {

    public List<Integer> getRow(int rowIndex) {
        List<List<Integer>> generate = generate(rowIndex + 1);
        return generate.get(rowIndex);
    }

    private List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            List<Integer> list = new ArrayList<>();
            ans.add(list);
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    list.add(1);
                } else {
                    List<Integer> lastList = ans.get(i - 1);
                    int val = lastList.get(j) + lastList.get(j - 1);
                    list.add(val);
                }
            }
        }
        return ans;
    }
}
